package com.financing.wap.util;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.financing.wap.common.CsvExportable;


/**
 * 导出Csv 工具类
 * @author lisu
 *
 */
public class CsvExportUtil {

	protected static Logger log = Logger.getLogger(CsvExportUtil.class);

	/**
	 * Export records to csv file
	 * 
	 * @param response
	 *            Http response
	 * @param items
	 *            List of item that implements on CsvExportable interface
	 * @throws IOException
	 */
	public static void doCsvExport(HttpServletResponse response,
			List<CsvExportable> items, String fileName) throws IOException {
		// reset response buffer and header information
		log.info("in doCsvExport results to csv");
		String t = new String( fileName.getBytes("gb2312"), "ISO8859-1" );
		response.reset();
		response.setHeader("Content-Disposition",
				"attachment;filename=" + t);
		OutputStreamWriter osw = null;
		try {
			osw = new OutputStreamWriter(response.getOutputStream(), "GB2312"); 
			// csv bom 
			osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));  
			for (int i = 0; i < items.size(); i++) {
				CsvExportable item = items.get(i);
				
				if (i == 0) {
					osw.write(item.csvTitle());
					osw.write("\r\n");
				}
				osw.write(item.toCsvString());
				osw.write("\r\n");
				if ((i + 1) % 100 == 0) {
					osw.flush();  
				}
				
			}
		} catch (IOException e) {
			log.error("Exception happens when export results to csv", e);
			throw e;
		} finally {
			if (osw != null) {
				osw.close();
			}
		}
	}
	
	public static void doCsvExportForZip(ZipOutputStream zipOutputStream,
			List<CsvExportable> items, String zipEntryName) throws IOException {
		log.info("in doCsvExportForZip results to csvZip");
		try {
			
			ZipEntry zipEntry = new ZipEntry(zipEntryName);

			zipOutputStream.putNextEntry(zipEntry);
			// csv bom
			zipOutputStream.write(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF });
			for (int i = 0; i < items.size(); i++) {

				CsvExportable item = items.get(i);

				if (i == 0) {
					zipOutputStream.write(item.csvTitle().getBytes("GB2312"));
					zipOutputStream.write("\r\n".getBytes("GB2312"));
				}
				zipOutputStream.write(item.toCsvString().getBytes("GB2312"));
				zipOutputStream.write("\r\n".getBytes("GB2312"));
				if ((i + 1) % 100 == 0) {
					zipOutputStream.flush();
				}

			}
		} catch (IOException e) {
			log.error("Exception happens when export results to csv", e);
			throw e;
		} 
	}
}
